package com.ganyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ganyu.component.RedisCache;
import com.ganyu.domain.LoginUser;
import com.ganyu.domain.ResponseResult;
import com.ganyu.domain.vo.UserInfoVo;
import com.ganyu.exception.SystemException;
import com.ganyu.mapper.UserMapper;
import com.ganyu.domain.entity.User;
import com.ganyu.service.UserService;
import com.ganyu.utils.BeanCopyUtils;
import com.ganyu.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.ganyu.enums.AppHttpCodeEnum.*;

/**
 * @author chenxi
 * @since 2022-09-16 13:11:11
 * @Description 用户个人信息查询
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private PasswordEncoder passwordEncoder;

    //查看用户信息
    @Override
    public ResponseResult<UserInfoVo> getUserInfo() {
        //1. 获取用户id
        Long userId = SecurityUtils.getUserId();
        //2. 根据用户id从redis中获取用户信息
        LoginUser loginUser = redisCache.getCacheObject("userId:" + userId);
        User user = loginUser.getUser();
        //3. 将用户信息封装为UserInfoVo
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        return ResponseResult.okResult(userInfoVo);
    }

    //修改用户信息
    @Override
    public ResponseResult<UserInfoVo> updateUserInfo(User user) {
        //修改数据库中的用户信息
        userMapper.updateById(user);
        //修改redis中的用户信息
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
        query.eq(User::getId,user.getId());
        LoginUser loginUser = new LoginUser();
        loginUser.setUser(user);
        redisCache.setCacheObject("userId:" + user.getId(),loginUser);
        return ResponseResult.okResult();
    }

    //用户注册
    @Override
    public ResponseResult<UserInfoVo> userRegister(User user) {
        //用户名不能为空
        if(!StringUtils.hasText(user.getUserName())){
            throw new SystemException(REQUIRE_USERNAME);
        }
        //昵称不能为空
        if(!StringUtils.hasText(user.getNickName())){
            throw new SystemException(REQUIRE_NICKNAME);
        }
        //邮箱不能为空
        if(!StringUtils.hasText(user.getEmail())){
            throw new SystemException(REQUIRE_EMAIL);
        }
        //密码不能为空
        if(!StringUtils.hasText(user.getPassword())){
            throw new SystemException(REQUIRE_PASSWORD);
        }
        //用户名不能相同
        User user1 = userMapper.selectUserName(user.getUserName());
        if(!ObjectUtils.isEmpty(user1)){
            throw new SystemException(USERNAME_EXIST);
        }
        //昵称不能相同
        User user2 = userMapper.selectNickName(user.getNickName());
        if(!ObjectUtils.isEmpty(user2)){
            throw new SystemException(NICKNAME_EXIST);
        }
        //邮箱不能相同
        String user3 = userMapper.selectEmail(user.getEmail());
        if(!ObjectUtils.isEmpty(user3)){
            throw new SystemException(EMAIL_EXIST);
        }
        //对密码进行加密
        String encodePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePassword);
        userMapper.insert(user);
        return ResponseResult.okResult();
    }


}

